package com.wyhello.tube4.dao.impl;

import java.math.BigInteger;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.wyhello.tube4.dao.BagRecordsDAO;
import com.wyhello.tube4.pojo.Bag;

public class BagRecordsDAOImpl  implements BagRecordsDAO{
	private SessionFactory sessionFactory;
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	/**
	 * 实测记录包
	 * @param bag 包
	 * @return 返回是否录入成功
	 */
	@Override
	public boolean saveBag(Bag bag) {
		Session session = sessionFactory.getCurrentSession();
		int i = 1;
		try{
			session.save(bag);
		}catch(Exception e){
			i=0;
		}
		if(i==1){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 对包进行批量删除
	 * @param ids 要删除的id
	 * @return 返回删除的条数
	 */
	@Override
	public int deleteBag(String ids) {
		Session session  =  sessionFactory.getCurrentSession();
		String sql = "delete from bag where id in "+ids;
		int i = session.createSQLQuery(sql).executeUpdate();
		return i;
	}
	/**
	 * 查询所有包
	 * @return 返回所有包
	 */
	@Override
	public List<Bag> queryBag(int resideId) {
		Session session  =  sessionFactory.getCurrentSession();
		String hql = "from Bag where resideId = " + resideId +" order by id desc ";
		List<Bag> list = session.createQuery(hql).list();
		return list;
	}
	/**
	 * 根据id查询换管记录信息
	 * @param id 要查询的id
	 * @return 为ID的换管记录信息
	 */
	@Override
	public Bag getBagById(int id) {
		Session session  =  sessionFactory.getCurrentSession();
		Bag bag = (Bag) session.load(Bag.class, Integer.valueOf(id));
		return bag;
	}
	/**
	 * 对包进行修改
	 * @param bag 修改后的信息
	 * @return 修改是否成功
	 */
	@Override
	public boolean updateBag(Bag bag) {
		Session session  =  sessionFactory.getCurrentSession();
		try{
			session.update(bag);
		}catch(Exception e){
			return false;
		}
		return true;
	}
	/**
	 * 查询最大ID值
	 * @param positionId 
	 * @return 
	 */
	@Override
	public int queryBagMaxId(int resideId) {
		Session session  =  sessionFactory.getCurrentSession();
		String sql = "select max(id) from bag where resideId = " + resideId;
		int id = (Integer) session.createSQLQuery(sql).list().get(0);
		return id;
	}
}
